from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Permission, ContentType, Group
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response

from users.models import User
from .serializers import *


# 分页器
class PageNum(PageNumberPagination):
    page_query_param = 'page'  # ?page=1
    page_size_query_param = 'pagesize'  # ?pagesize=10
    max_page_size = 100  # 每页按照最大100个划分
    page_size = 10  # 默认按照10个/页划分

    def get_paginated_response(self, data):
        return Response({
            "counts": self.page.paginator.count,
            "lists": data,
            "page": self.page.number,
            "pages": self.page.paginator.num_pages,
            "pagesize": self.page_size,
        })

    # Create your views here.


class PermissionView(ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer

    pagination_class = PageNum

    def get_queryset(self):
        return self.queryset.order_by('pk')

    def content_types(self, request):
        content = ContentType.objects.all()

        ser = ContentTypeSerialzier(content, many=True)
        return Response(ser.data)


class ContentTypeView(ListAPIView):
    queryset = ContentType.objects.all()
    serializer_class = ContentTypeSerialzier


class PermissionSimpleView(ListAPIView):
    queryset = Permission.objects.all()
    serializer_class = PermissionSimpleSerializer


class GroupView(ModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerialzier

    pagination_class = PageNum

    # 获取权限表数据
    def simple(self, reqeust):
        pers = Permission.objects.all()
        ser = PermissionSerializer(pers, many=True)  # 使用以前定义的全选序列化器
        return Response(ser.data)
class GroupPermListView(ListAPIView):
    queryset = Permission.objects.all()
    serializer_class = PermissionSimpleSerializer

class AdminView(ModelViewSet):
    queryset = User.objects.filter(is_staff=True)
    serializer_class = AdminSerialzier

    pagination_class = PageNum

    # # 获取权限表数据
    # def simple(self, reqeust):
    #     pers = Group.objects.all()
    #     ser = GroupSerialzier(pers, many=True)  # 使用以前定义的全选序列化器
    #     return Response(ser.data)

class AdminSimpleView(ListAPIView):
    queryset = Group.objects.all()
    serializer_class = AdminGroupSerializer
